//
//  FeastMenu.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/12.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

protocol FeastMenuDelegate {
    func showFeastDetail(feast: RestaurantFeast)
}

class FeastMenu: UIView {

    var delegate: FeastMenuDelegate?
    
    var feast: RestaurantFeast?
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.black
        return label
    }()
    
    let priceLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.gray
        return label
    }()
    
    let pointerLabel: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(named: "pointer")
        return imageView
    }()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(titleLabel)
        self.addSubview(priceLabel)
        self.addSubview(pointerLabel)
        self.addSubview(saperator)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            delegate?.showFeastDetail(feast: feast!)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let pointerWidth:CGFloat = 13
        let textMaxSize = CGSize(width: bounds.width, height: 40)
        let titleWidth = TextSize.textSize(text: titleLabel.text!, font: AppFont(size: 14), maxSize: textMaxSize).width
        let priceWidth = TextSize.textSize(text: priceLabel.text!, font: AppFont(size: 14), maxSize: textMaxSize).width
        titleLabel.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, 10, 5, bounds.width - 10 - titleWidth))
        priceLabel.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, bounds.width - pointerWidth - 15 - priceWidth, 5, 10 + pointerWidth + 5))
        pointerLabel.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, bounds.width - 10 - pointerWidth, 5, 10))
        saperator.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 0.5, 10, 0, 10))
    }

}

class MenuHeader: UIView {

    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.black
        label.text = LanguageHelper.getString(key: "FEAST_MENU")
        return label
    }()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(titleLabel)
        self.addSubview(saperator)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    } 
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let textMaxSize = CGSize(width: bounds.width, height: 40)
        let titleWidth = TextSize.textSize(text: titleLabel.text!, font: AppFont(size: 14), maxSize: textMaxSize).width
        titleLabel.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, 10, 5, bounds.width - 10 - titleWidth))
        saperator.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 0.5, 10, 0, 10))
    }
}

class RestaurantPromotion: UIView {
    
    let profilePic: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    let name: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 12)
        label.numberOfLines = 0
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
        self.addSubview(profilePic)
        self.addSubview(name)
    }
    
    func initValue(restaurant: Restaurant) {
        self.profilePic.image = restaurant.profilePic
        self.name.text = restaurant.name
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        profilePic.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, bounds.height - bounds.width, 0))
        let nameMaxSize = CGSize(width: bounds.width - 4 , height: 75)
        let nameSize = TextSize.textSize(text: name.text!, font: AppFont(size: 12), maxSize: nameMaxSize)
        name.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.width + 5, 2, bounds.height - bounds.width - 5 - nameSize.height, 2))
    }
}
